﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLibrary;

namespace BusinessLayer
{
    public class ClsLeaveTypes
    {
        public string LeaveType { get; set; }
        public int LeaveTypeID { get; set; }

        public DataTable GetAllLeaveTypes()
        {
            using (OleDataClient data = new OleDataClient())
            {
                return data.GetDataTable("Select * from LeaveTypes", "LeaveTypes");
            }
        }
        public void GetLeaveType(int LeaveTypeID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select * from LeaveTypes where LeaveTypeID=" + LeaveTypeID.ToString(), "LeaveTypes");
                if (dt.Rows.Count > 0)
                {
                    LeaveTypeID = Convert.ToInt32(dt.Rows[0]["LeaveTypeID"]);
                    LeaveType = dt.Rows[0]["LeaveType"].ToString();
                }
            }
        }
        public bool SaveLeaveType(string KeyForSave)
        {
            using (OleDataClient data = new OleDataClient())
            {
                if (KeyForSave == "add")
                {
                    string count = data.GetValue("select count(*) from LeaveTypes where LeaveType='" + LeaveType + "'");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Leave Type already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Insert into LeaveTypes (LeaveType) values('" + LeaveType  + "')");
                    }
                }
                else
                {
                    string count = data.GetValue("select count(*) from LeaveTypes where LeaveType='" + LeaveType + "' and LeaveTypeID NOT IN (" + LeaveTypeID.ToString() + ")");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Leave Type already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Update LeaveTypes set LeaveType = '" + LeaveType + "' where LeaveTypeID=" + LeaveTypeID.ToString());
                    }

                }
            }
            return true;
        }
        public void DeleteLeaveType()
        {
            using (OleDataClient data = new OleDataClient())
            {
                data.ExecuteNonQuery("Delete from  LeaveTypes where LeaveTypeID=" + LeaveTypeID.ToString());
            }
        }
    }



}